package cn.shyouchi.wuguanjia.android.ui.event.post;

import android.content.Context;
import android.support.annotation.NonNull;

import java.util.List;

import cn.shyouchi.wuguanjia.android.api.event.EventApi;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Eenie on 2016/11/16 at 10:58
 * Email: 472279981@qq.com
 * Des:
 */

public class EventPostPresenter implements EventPostContract.Presenter {


    private Context mContext;
    private EventApi mEventApi;
    EventPostContract.View mView;

    public EventPostPresenter(Context context, EventPostContract.View view) {
        this.mContext = context;
        mEventApi = new EventApi(context);
        this.mView = view;
    }

    @Override
    public void getPostList(String projectId) {
        mEventApi.getPostList(projectId)
                .subscribeOn(Schedulers.io())
                .map(new Func1<PostEntity, List<PostEntity.ResultMessageBean>>() {
                    @Override
                    public List<PostEntity.ResultMessageBean> call(PostEntity postEntity) {
                        return postEntity.getResultMessage();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<List<PostEntity.ResultMessageBean>>(mContext) {

                    @Override
                    public void onNext(List<PostEntity.ResultMessageBean> resultMessageBeen) {
                        mView.onDataRefresh(resultMessageBeen);
                    }
                });
    }

    @Override
    public void delPostList(final String projectId, String id) {
        mEventApi.delPost(id)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
            @Override
            public void onNext(BaseRequestResult result) {
                getPostList(projectId);
            }
        });
    }

    @Override
    public void attachView(@NonNull EventPostContract.View view) {

    }

    @Override
    public void detachView() {

    }
}
